#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

class Solution {
public:
    int findPeakElement(vector<int>& nums) {
        //使用二分法查找
        //先确定边界，边界如不为极值，则极值必在中间
        if(nums.size()==1){
            return 0;
        }
        if(nums[0]>nums[1]){
            return 0;
        }
        int size = nums.size();
        if(nums[size-1]>nums[size-2]){
            return size -1;
        }
        int left = 0;
        int right = size -1;
        while(left<=right){
            int mid = (left+right)/2;
            if(nums[mid-1]<nums[mid]&&nums[mid]>nums[mid+1]){
                return mid;
            }
            else if(nums[mid-1]>nums[mid]){
                right = mid;
            }
            else if(nums[mid]<nums[mid+1]) {
                left = mid;
            }
        }
        return 0;
        
    }
};

int main(){
    Solution S;
    vector<int> nums = {3,4,3,2,1};
    cout<<S.findPeakElement(nums)<<endl;
}
